package com.mmche.web.controller;



import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.easycoding.framework.annotation.Token;
import com.framework.base.util.Tool;
import com.framework.support.domain.PageMan;
import com.framework.ums.common.CommonService;
import com.framework.web.springmvc.BaseController;
import com.mmche.base.service.BrandService;
import com.mmche.base.service.ModelsService;
import com.mmche.car.pojo.Exchange;
import com.mmche.car.service.ExchangeService;
import com.mmche.car.service.NewCarSaleService;
import com.mmche.common.Constant;
import com.mmche.purchase.pojo.PurchaseOnlineOrder;
import com.mmche.purchase.service.PurchaseOnlineOrderService;

@Controller
public class SellController extends BaseController{
	
	@Resource(name="webBrandService")
	private BrandService brandService;
	
	@Resource
	private ModelsService modelsService;
	
	@Resource
	private PurchaseOnlineOrderService purchaseOnlineOrderService;
	
	@Resource
	private NewCarSaleService newCarSaleService;
	
	@Resource
	private ExchangeService exchangeService;
	
	/**
	 * 我要卖车
	 * @param returnValue
	 * @return
	 */
	@RequestMapping("/sellcar.html")
	@Token(create = true)
	private String sell(Map<String,Object> returnValue){
		returnValue.put("location", "sellcar");
		returnValue.put("brands", this.brandService.getCarBrands());
		returnValue.put("colors", CommonService.getInstance().getDics(Constant.DIC_CAR_COLOR));
		returnValue.put("trans", CommonService.getInstance().getDics(Constant.DIC_TRANSMISSION_TYPE));
		int year =Integer.parseInt(Tool.dateToString(new Date(),"yyyy"));
		List<Integer> yearList = new ArrayList<Integer>();
		for(int i=0;i<20;i++){
			yearList.add(year-19+i);
		}
		returnValue.put("years",yearList);
		return "sell/sell";
	}
	
	/**
	 * 保存卖车信息
	 * @param order
	 * @return
	 */
	@RequestMapping("/sellcar/save.html")
	@Token(validate = true)
	public String save(PurchaseOnlineOrder order){
		String brand = this.getRequestParameter("brandId");
		String series = this.getRequestParameter("seriesId");
		if(!Tool.isEmpty(brand))
			order.setBrand(this.brandService.findBySpell(brand));
		if(!Tool.isEmpty(series))
			order.setModels(this.modelsService.getModelsById(series));
		String orderId = this.purchaseOnlineOrderService.save(order);
		return "redirect:/sellcar/success/"+orderId+".html";
	}
	
	/**
	 * 卖车提交成功
	 * @param orderId
	 * @param returnValue
	 * @return
	 */
	@RequestMapping("/sellcar/success/{orderId}.html")
	public String saveSuccess(@PathVariable String orderId,Map<String,Object> returnValue){
		returnValue.put("location", "sellcar");
		returnValue.put("orderId", orderId);
		return "sell/success";
	}
	
	/**
	 * 我要置换
	 * @param returnValue
	 * @return
	 */
	@RequestMapping("/zhihuan.html")
	@Token(create =true)
	public String zhihuan(Map<String,Object> returnValue){
		returnValue.put("location", "exchange");
		returnValue.put("brands", this.brandService.getCarBrands());
		int year =Integer.parseInt(Tool.dateToString(new Date(),"yyyy"));
		List<Integer> yearList = new ArrayList<Integer>();
		for(int i=0;i<20;i++){
			yearList.add(year-19+i);
		}
		returnValue.put("years",yearList);
		PageMan pageMan = new PageMan();
		pageMan.setPage(false);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("show", true);
		returnValue.put("news", this.newCarSaleService.getCarPageByMap(map, pageMan));
		return "sell/exchange";
	}
	
	/**
	 * 提交置换报名
	 * @param exchange
	 * @return
	 */
	@RequestMapping("/zhihuan/save.html")
	@Token(validate = true)
	public String saveExchange(Exchange exchange){
		String oldBrand = this.getRequestParameter("oldBrandId");
		String oldSeries = this.getRequestParameter("oldSeries");
		String newBrand = this.getRequestParameter("newBrandId");
		String newSeries = this.getRequestParameter("newSeries");
		if(!Tool.isEmpty(oldBrand))
			exchange.setOldBrand(this.brandService.findBySpell(oldBrand));
		if(!Tool.isEmpty(oldSeries))
			exchange.setOldModel(this.modelsService.getModelsById(oldSeries));
		if(!Tool.isEmpty(newBrand))
			exchange.setNewBrand(this.brandService.findBySpell(newBrand));
		if(!Tool.isEmpty(newSeries))
			exchange.setNewModel(this.modelsService.getModelsById(newSeries));
		this.exchangeService.saveExchange(exchange);
		return "redirect:/zhihuan/success.html";
	}
	
	/**
	 * 提交置换报名结果
	 */
	@RequestMapping("/zhihuan/success.html")
	public String exchageResult(Map<String,Object> returnValue){
		returnValue.put("location", "exchange");
		return "sell/exchange_result";
	}
}